<?php

class Market extends AppModel {

	const MT_DEMAND = 1;
	const MT_OFFER = 2;

	const STATUS_NEW = 0;
	const STATUS_IN_PROCESS = 1;
	const STATUS_FINISHED_SUCCESSFULLY = 2;
	const STATUS_DECLINED = 3;
	
	var $name = 'Market';

	var $useTable = 'mnhis_market';

	protected $fields = array(
		'id' => array('name' => 'ID'),
		'description' => array('name' => 'Popis'),
		'type' => array('name' => 'Typ', 'evaluation' => array('1' => 'dopyt', '2' => 'ponuka', 'default' => 'neznámy')),
		'fk_client_id' => array('name' => 'Klient', 'reference' => array('model' => 'Person', 'field' => 'full_name', 'default' => 'Nikto')),
		'fk_tipster_id' => array('name' => 'Tiper', 'reference' => array('model' => 'Person', 'field' => 'full_name', 'default' => 'Nikto')),
		'fk_broker_id' => array('name' => 'Maklér', 'reference' => array('model' => 'Person', 'field' => 'full_name', 'default' => 'Nikto')),
		'commission' => array('name' => 'Provízia'),
		'status' => array('name' => 'Stav', 'evaluation' => array(
				self::STATUS_NEW => 'čaká na evidovanie',
				self::STATUS_IN_PROCESS => 'v procese',
				self::STATUS_FINISHED_SUCCESSFULLY => 'úspešne ukončené',
				self::STATUS_DECLINED => 'zamietnuté',
			)
		)
	);
	
	var $belongsTo = array(
		'Client' => array(
			'className' => 'Person',
			'foreignKey' => 'fk_client_id',
		),
		'Tipster' => array(
			'className' => 'Person',
			'foreignKey' => 'fk_tipster_id',
		),
		'Broker' => array(
			'className' => 'Person',
			'foreignKey' => 'fk_broker_id',
		)
	);

	var $validate = array(
        'description' => array(
			'rule' => array('minLength', 1),
            'required' => TRUE,
			'message' => 'Musíte vyplniť description',
		),
		'type' => array(
			'rule' => array('comparison', '>', 0),
			'message' => 'Musíte vybrať typ'
		),
	);
	
	var $relations = array(
		'Transfer' => array(
			'fk_offer_id' => array(
				'field' => 'id',
				'message_one' => 'Obrat: ###PLACEHOLDER### musí byť najprv zmazaný alebo mu musí byť priradená iná ponuka',
				'message_more' => 'Obraty: ###PLACEHOLDER### musia byť najprv zmazané alebo im musia byť priradené iné ponuky',
			),
			'fk_demand_id' => array(
				'field' => 'id',
				'message_one' => 'Obrat: ###PLACEHOLDER### musí byť najprv zmazaný alebo mu musí byť priradený iný dopyt',
				'message_more' => 'Obraty: ###PLACEHOLDER### musia byť najprv zmazané alebo im musia byť priradené iné dopyty',
			),
		),
	);
	
	public function __construct($id = false, $table = null, $ds = null) {
		parent::__construct($id, $table, $ds);

		// nahradime m2 za realne metre stvorcove
		$this->virtualFields['html_description'] = sprintf('REPLACE(%s.description, "m2", "m<sup>2</sup>")', $this->alias);
	}
	
	public static function getStatusList() {
		$statusList = array(
			self::STATUS_NEW => 'čaká na evidovanie',
			self::STATUS_IN_PROCESS => 'v procese',
			self::STATUS_FINISHED_SUCCESSFULLY => 'úspešne ukončené',
			self::STATUS_DECLINED => 'zamietnuté',
		);
		return $statusList;
	}
	
	public static function getStatusText($status) {
		$statusList = self::getStatusList();
		
		if (isset($statusList[$status])) {
			return $statusList[$status];
		} else {
			return 'Neznámy';
		}
	}
	
	public static function getStatusColor($status) {
		$statusList = array(
			self::STATUS_NEW => 'orange',
			self::STATUS_IN_PROCESS => 'green',
			self::STATUS_FINISHED_SUCCESSFULLY => 'blue',
			self::STATUS_DECLINED => 'red',
		);
		
		if (isset($statusList[$status])) {
			return $statusList[$status];
		} else {
			return 'black';
		}
	}
}

?>